<template>
  <div class="member-container">
    <el-card class="header">
      <div>
        <el-button type="success" @click="memberAdd">{{
          $t('msg.member.memberAdd')
        }}</el-button>

        <el-button
          class="float-right"
          type="primary"
          @click="onImportExcelClick"
          v-permission="['treatDiscountSet']"
          >{{ $t('msg.member.importExcel') }}</el-button
        >
        <el-button
          class="float-right"
          type="warning"
          @click="onImportExcelClick"
          v-permission="['treatDiscountSet']"
          >{{ $t('msg.member.exportExcel') }}</el-button
        >
        <el-button
          class="float-right"
          type="danger"
          @click="onImportExcelClick"
          v-permission="['treatDiscountSet']"
          >{{ $t('msg.member.batchDelete') }}</el-button
        >
      </div>
    </el-card>
    <el-card class="table-container">
      <el-table :data="tableData" border style="width: 100%" row-key="id">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          align="center"
          :label="$t('msg.member.cardNumber')"
          prop="cardNumber"
        ></el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.member.name')"
          prop="realName"
        ></el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.member.phone')"
          prop="phone"
        ></el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.member.classifyName')"
          prop="classifyName"
        ></el-table-column>
        <el-table-column align="center" :label="$t('msg.member.balanceMoney')">
          <template #default="{ row }">
            {{ $filters.moneyFilterShow(row.balanceMoney) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="$t('msg.member.presentBalanceMoney')"
        >
          <template #default="{ row }">
            {{ $filters.moneyFilterShow(row.presentBalanceMoney) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('msg.member.action')"
          fixed="right"
          width="300px"
          align="center"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="onShowDetailClick(row.id)"
              >{{ $t('msg.member.detail') }}</el-button
            >
            <el-button type="danger" size="mini" @click="onRemoveClick(row)">{{
              $t('msg.member.removeMember')
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-size="size"
        :total="total"
        :page-sizes="[10, 20]"
        layout="total,sizes,prev,pager,next,jumper"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { memberList, deleteMemberClassify } from '@/api/member'
import { watchSwitchLang } from '@/utils/i18n'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(10)
const getMemberList = async () => {
  const result = await memberList({
    page: page.value,
    size: size.value
  })
  tableData.value = result.items
  total.value = result.total
}

getMemberList()
onActivated(getMemberList)
watchSwitchLang(getMemberList)
const router = useRouter()
// 查看用户详情

const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getMemberList()
}
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getMemberList()
}

const onShowDetailClick = (id) => {
  // 查看用户详情
  router.push(`/member/detail/${id}`)
}

const memberAdd = () => {
  router.push('/member/add')
}
const i18n = useI18n()
const onRemoveClick = (row) => {
  ElMessageBox.confirm(
    i18n.t('msg.member.dialogTitle1') +
      row.realName +
      i18n.t('msg.member.dialogTitle2'),
    {
      confirmButtonText: i18n.t('msg.universal.confirm'),
      type: 'warning',
      cancelButtonText: i18n.t('msg.universal.cancel')
    }
  ).then(async () => {
    await deleteMemberClassify(row.id)
    ElMessage.success(i18n.t('msg.excel.removeSuccess'))
    getMemberList()
  })
}
</script>

<style lang="scss" scoped>
.member-container {
  .header {
    margin-bottom: 22px;
    text-align: left;
    .float-right {
      float: right;
    }
  }
  .table-container {
    .pagination {
      display: flex;
      margin-top: 20px;
      justify-content: center;
    }
  }
}
</style>
